<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"/>
	<title>Test</title>
</head>
<body>
<div id="buy">立即购买</div>
<div id="add">加入购物车</div>

<script>
function android_ios_switch() {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if(isAndroid){
        return 'Android';
    }else if(isiOS){
        return 'IOS';
    }else{
        return '';
    }
}
var buy = document.getElementById('buy');
var add = document.getElementById('add');
var device = android_ios_switch();
var parm = {
	product_id: "1",
	num: 2,
    type: 2
};
// <!-- IOS申明交互 -->
function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'https://__bridge_loaded__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
                          
  //oc 吊 js
function getRegistrationIdFromJpush(message) {
    alert(message);
}
//立即购买
buy.addEventListener('click', function() {
	if(device === 'Android' && typeof AndroidFun === 'object' && AndroidFun.hasOwnProperty('Buy')){
	    AndroidFun.Buy(parm.product_id, parm.num);

	}else if(device === 'IOS'){
	    // <!-- 处理交互  方法名要和ios内定义的对应-->
	    setupWebViewJavascriptBridge(function(bridge) {
	            // <!--处理 js 调用 oc -->
	            bridge.callHandler('showInfoFromJs', parm, function(response) {
	                 //处理oc过来的回调
	                 alert('收到oc过来的回调:'+response)
	            })
	        // }
	    })

	}else{
	    alert('处理失败!');
	    window.location.reload();
	}
}, false);
// 加入购物车
add.addEventListener('click', function() {
	if(device === 'Android' && typeof AndroidFun === 'object' && AndroidFun.hasOwnProperty('delAliasFromJs')){
	    AndroidFun.AddShopCart(parm.product_id, parm.num);

	}else if(device === 'IOS'){
	    // <!-- 处理交互  方法名要和ios内定义的对应-->
	    setupWebViewJavascriptBridge(function(bridge) {
	            // <!--处理 js 调用 oc -->
	            bridge.callHandler('delAliasFromJs', parm, function(response) {
	                 //处理oc过来的回调
	                 alert('收到oc过来的回调:'+response)
	            })
	        // }
	    })

	}else{
	    alert('处理失败!');
	    window.location.reload();
	}
}, false);
                          
                          
</script>
</body>
</html>
